package com.george.shiro.model.controller;

import com.george.shiro.model.entity.Resources;
import com.george.shiro.model.service.IResourcesService;
import com.george.shiro.model.service.IRoleResourcesService;
import com.george.shiro.model.service.ShiroService;
import com.george.shiro.result.PageResult;
import com.george.shiro.result.ResponseVO;
import com.george.shiro.util.ResultUtil;
import com.george.shiro.vo.ResourceConditionVO;
import com.george.shiro.vo.ResourcesVo;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *
 * </p>
 *
 * @author GeorgeChan 2019/5/26 16:40
 * @version 1.0
 * @since jdk1.8
 */
@RestController
@RequestMapping("/resources")
public class ResourceController {
    private final IResourcesService resourcesService;
    private final IRoleResourcesService roleResourcesService;
    private final ShiroService shiroService;

    @Autowired
    public ResourceController(IResourcesService resourcesService, IRoleResourcesService roleResourcesService, ShiroService shiroService) {
        this.resourcesService = resourcesService;
        this.roleResourcesService = roleResourcesService;
        this.shiroService = shiroService;
    }

    /**
     * 资源列表
     * @param vo
     * @return
     */
    @RequiresPermissions("resources")
    @PostMapping("/list")
    public PageResult getAll(ResourceConditionVO vo) {
        PageInfo<ResourcesVo> pageInfo = resourcesService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    /**
     * 新增资源
     * @param resources
     * @return
     */
    @RequiresPermissions("resource:add")
    @PostMapping(value = "/add")
    public ResponseVO add(Resources resources) {
        resourcesService.save(resources);
        //更新权限
        shiroService.updatePermission();
        return ResultUtil.success("成功");
    }

    /**
     * 获取某角色的资源权限
     * @param rid
     * @return
     */
    @RequiresPermissions("role:allotResource")
    @PostMapping("/resourcesWithSelected")
    public ResponseVO resourcesWithSelected(Long rid) {
        return ResultUtil.success(null, resourcesService.queryResourcesListWithSelected(rid));
    }
}
